<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="商户手机号" prop="shoperMobile">
        <el-input
          v-model="queryParams.shoperMobile"
          placeholder="请输入商户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cancel:shoper:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cancel:shoper:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="shoperList"
      row-key="shId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="shoperName"
        label="商户名称"
        width="260"
      ></el-table-column>
      <el-table-column
        prop="shoperMobile"
        label="商户手机号"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="shoperDec"
        label="商户简介"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="shoperSite"
        label="商户地址"
        width="200"
      ></el-table-column>

      <el-table-column label="是否启用" align="center" prop="isStatus">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.parentId != 0"
            v-model="scope.row.isStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addChildren(scope.row)"
            v-if="scope.row.parentId == 0"
            v-hasPermi="['cancel:shoper:addChildren']"
            >添加核销人员</el-button
          >
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="editChildren(scope.row)"
            v-hasPermi="['cancel:shoper:editChildren']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改核销商户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商户名称" prop="shoperName">
          <el-input v-model="form.shoperName" placeholder="请输入商户名称" />
        </el-form-item>
        <!--        <el-form-item label="商户手机号" prop="shoperMobile">-->
        <!--          <el-input-->
        <!--            v-model="form.shoperMobile"-->
        <!--            placeholder="请输入商户手机号"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item label="商户简介" prop="shoperDec">
          <el-input v-model="form.shoperDec" placeholder="请输入商户简介" />
        </el-form-item>
        <el-form-item label="商户位置" prop="shoperSite">
          <el-input v-model="form.shoperSite" placeholder="请输入商户位置" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改核销商户对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="childrenOpen"
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="父级商户" prop="parentId">
          <el-select v-model="form.parentId" placeholder="选择商户">
            <el-option
              v-for="item in shoperList1"
              :key="item.shId"
              :label="item.shoperName"
              :value="item.shId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="核销人员" prop="shoperName">
          <el-input v-model="form.shoperName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="商户手机号" prop="shoperMobile">
          <el-input
            v-model="form.shoperMobile"
            placeholder="请输入商户手机号"
          />
        </el-form-item>
        <!--        <el-form-item label="商户简介" prop="shoperDec">-->
        <!--          <el-input v-model="form.shoperDec" placeholder="请输入商户简介" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="商户位置" prop="shoperSite">-->
        <!--          <el-input v-model="form.shoperSite" placeholder="请输入商户位置" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="childrenCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listShoper,
  getShoper,
  delShoper,
  addShoper,
  updateShoper,
  changeUpdateIsShelf,
  showShoperList,
} from "@/api/system/shoper";

export default {
  name: "Shoper",
  data() {
    const validateMobile = function (rule, value, callback) {
      const reg = /^1[3456789]\d{9}$/;
      if (reg.test(value)) {
        callback();
      } else {
        return callback(new Error("请输入正确的手机号"));
      }
    };
    return {
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 核销商户表格数据
      shoperList: [],
      shoperList1: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      childrenOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shoperMobile: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shoperName: [
          { required: true, message: "请输入商户名称", trriger: "blur" },
        ],
        shoperMobile: [
          { required: true, message: "请输入用户手机号", trriger: "blur" },
          { validator: validateMobile, trriger: "blur" },
        ],
        shoperDec: [
          { required: true, message: "请输入商户简介", trriger: "blur" },
        ],
        shoperSite: [
          { required: true, message: "请输入商户位置", trriger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取关联商户列表
    getShoperList() {
      showShoperList().then((res) => {
        this.shoperList1 = res.data;
      });
    },

    /** 查询核销商户列表 */
    getList() {
      this.loading = true;
      listShoper(this.queryParams).then((response) => {
        this.shoperList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    childrenCancel() {
      this.childrenOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shId: null,
        shoperName: null,
        shoperMobile: null,
        shoperDec: null,
        shoperSite: null,
        shoperImg: null,
        shoperAccount: null,
        shoperPassword: null,
        pwdM: null,
        isStatus: null,
        createTime: null,
        parentId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.shId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加核销商户";
    },

    addChildren(row) {
      this.getShoperList();
      console.log(row.shId);
      this.reset();
      this.childrenOpen = true;
      this.form.parentId = row.shId;
      this.title = "添加核销商户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shId = row.shId || this.ids;
      getShoper(shId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改核销商户";
      });
    },
    editChildren(row) {
      this.reset();
      const shId = row.shId || this.ids;
      getShoper(shId).then((response) => {
        this.form = response.data;
        this.childrenOpen = true;
        this.title = "修改核销商户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.shId != null) {
            updateShoper(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.childrenOpen = false;
              this.getList();
            });
          } else {
            addShoper(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.childrenOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    // 是否启用
    handleStatusChange(row) {
      console.log(row);
      const { shId, isStatus } = row;
      let text = row.isStatus == 1 ? "启用" : "关闭";
      this.$modal
        .confirm("确认要" + text + "" + "吗？")
        .then(function () {
          return changeUpdateIsShelf({ shId, isStatus });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isStatus = row.isStatus == 1 ? 0 : 1;
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shIds = row.shId || this.ids;
      this.$modal
        .confirm('是否确认删除核销商户编号为"' + shIds + '"的数据项？')
        .then(function () {
          return delShoper(shIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cancel/shoper/export",
        {
          ...this.queryParams,
        },
        `shoper_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
